package gov.kqqzyyy.monkey.orderpad.mytools;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.os.CountDownTimer;
import com.google.android.material.textfield.TextInputEditText;
import android.text.Editable;

import gov.kqqzyyy.monkey.orderpad.R;


/**
 * Created by Monkey on 2017/9/30.
 * 自定义软键盘处理类
 */

public class KeyBoardUtil {

    private TextInputEditText mTextInputEditText;
    private String trueValue;
    private String showText;
    private CountDownTimer countDownTimer;

    public KeyBoardUtil(Context context, KeyboardView keyboardView) {
//        Keyboard mKeyboard = new Keyboard(context, R.xml.keyboard_sfz);
        Keyboard mKeyboard = new Keyboard(context, R.xml.keyboard_sfz_hor);
        keyboardView.setKeyboard(mKeyboard);
        keyboardView.setPreviewEnabled(false);
        keyboardView.setEnabled(true);
        KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
            @Override
            public void onPress(int i) {

            }

            @Override
            public void onRelease(int i) {

            }

            @Override
            public void onKey(int keyCode, int[] ints) {
                boolean isAdd = false;
                Editable editable = mTextInputEditText.getText();
                showText = editable.toString().equals("null") ? "" : editable.toString();
                int len = editable.length();
                switch (keyCode) {
//                    case Keyboard.KEYCODE_CANCEL:
//                        hideKeyboard();
//                        break;
                    case Keyboard.KEYCODE_DELETE:
                        if (len > 0) {
                            editable.delete(len - 1, len);
                        }
                        int trueLen = trueValue.length();
                        if (trueLen > 0) {
                            trueValue = trueValue.substring(0, trueLen - 1);
                        }
                        break;
//                    case Keyboard.KEYCODE_DONE:
//                        hideKeyboard();
//                        break;
                    default:
                        isAdd = true;
                        editable.insert(len, Character.toString((char) keyCode));
                        showText = getShowText(showText.length()) + Character.toString((char) keyCode);
                        trueValue += Character.toString((char) keyCode);
                        editable.clear();
                        editable.append(showText);

                }
                if (isAdd) {
                    timeToBePWD();
                }
            }

            @Override
            public void onText(CharSequence charSequence) {

            }

            @Override
            public void swipeLeft() {

            }

            @Override
            public void swipeRight() {

            }

            @Override
            public void swipeDown() {

            }

            @Override
            public void swipeUp() {

            }
        };
        keyboardView.setOnKeyboardActionListener(listener);
        keyboardView.bringToFront();
        keyboardView.requestFocus();

    }

    public String getTrueValue() {
        return trueValue;
    }

    public void setTextInputEditText(TextInputEditText mTextInputEditText) {
        this.mTextInputEditText = mTextInputEditText;
        cleanValue();
    }

    public void cleanValue() {
        mTextInputEditText.setText("");
        trueValue = "";
    }


    private String getShowText(int len) {
        String empty = "";
        for (int i = 0; i < len; i++) {
            empty += "*";
        }
        return empty;
    }

//    private void hideKeyboard() {
//        int visibility = mKeyboardView.getVisibility();
//        if (visibility == View.VISIBLE) {
//            mKeyboardView.setVisibility(View.GONE);
//        }
//    }
//
//    private void showKeyboard() {
//        int visibility = mKeyboardView.getVisibility();
//        if (visibility == View.GONE || visibility == View.INVISIBLE) {
//            mKeyboardView.setVisibility(View.VISIBLE);
//        }
//    }

    private void timeToBePWD() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
            countDownTimer = null;
        }
        countDownTimer = new CountDownTimer(3000, 1000) {
            @Override
            public void onTick(long l) {

            }

            @Override
            public void onFinish() {
                Editable editable = mTextInputEditText.getText();
                String nowText = editable.toString();
                String pwdtext = getShowText(nowText.length());
                editable.clear();
                editable.append(pwdtext);
            }
        };
        countDownTimer.start();
    }
}
